import { app } from "../../../base";



/**
 * 日志类型
 */
export enum LogType {
    NONE = 0b000000, //不输出任何日志
    DEBUG = 0b000001, //只输出Debug日志
    INFO = 0b000010, //只输出info日志
    LOG = 0b000100, //只输出log日志
    WARN = 0b001000, //只输出warn日志
    ERROR = 0b010000, //只输出error日志
    ALL = 0b111111 //输出全部日志
}

/**
 * 日志输出管理,用于生产环境的日志控制.不需要手动创建.
 */
export class Logger {
    private static _reportError: boolean = false;
    /**
     * 设置app日志输出模式
     * @param logType 
     */
    static setLogType(logType: LogType) {
        if (logType != LogType.ALL) {
            console = window['console'] = new Logger(logType);
        }
    }

    static set reportError(value: boolean) {
        Logger._reportError = value;
    }

    private _logLevel: number;
    private _console;
    /**
     * 
     * @param logType 日志类型,可以用或(|)操作符, 传入多个日志类型.如: LogType.DEBUG | LogType.LOG
     */
    private constructor(logType: number) {
        this._logLevel = logType;
        this._console = console;
    }
    get memory(): any {
        return this._console.memory;
    }
    set memory(value: any) {
        this._console.memory = value;
    }
    get exception(): any {
        return this._console.exception;
    }
    set exception(value: any) {
        this._console.exception = value;
    }
    assert(condition?: boolean, ...data: any[]): void {
        if ((this._logLevel | LogType.ERROR) == this._logLevel) {
            this._console.assert(condition, ...data);
        }
    }
    clear(): void {
        this._console.clear();
    }
    count(label?: string): void {
        if ((this._logLevel | LogType.LOG) == this._logLevel) {
            this._console.count(label);
        }
    }
    countReset(label?: string): void {
        if ((this._logLevel | LogType.LOG) == this._logLevel) {
            this._console.countReset(label);
        }
    }
    debug(...data: any[]): void {
        if ((this._logLevel | LogType.DEBUG) == this._logLevel) {
            this._console.debug(...data);
        }
    }
    dir(item?: any, options?: any): void {
        this._console.dir(item, options);
    }
    dirxml(...data: any[]): void {
        this._console.dirxml(...data);
    }
    error(...data: any[]): void {
        if ((this._logLevel | LogType.ERROR) == this._logLevel) {
            this._console.error(...data);
        }
        app.publisher.analytics.error(JSON.stringify(data));
    }
    group(...data: any[]): void {
        if ((this._logLevel | LogType.LOG) == this._logLevel) {
            this._console.group(...data);
        }
    }
    groupCollapsed(...data: any[]): void {
        if ((this._logLevel | LogType.LOG) == this._logLevel) {
            this._console.groupCollapsed(...data);
        }
    }
    groupEnd(): void {
        if ((this._logLevel | LogType.LOG) == this._logLevel) {
            this._console.groupEnd();
        }
    }
    info(...data: any[]): void {
        if ((this._logLevel | LogType.INFO) == this._logLevel) {
            this._console.info(...data);
        }
    }
    log(...data: any[]): void {
        if ((this._logLevel | LogType.LOG) == this._logLevel) {
            this._console.log(...data);
        }
    }
    table(tabularData?: any, properties?: string[]): void {
        if ((this._logLevel | LogType.LOG) == this._logLevel) {
            this._console.table(tabularData, properties);
        }
    }
    time(label?: string): void {
        if ((this._logLevel | LogType.LOG) == this._logLevel) {
            this._console.time(label);
        }
    }
    timeEnd(label?: string): void {
        if ((this._logLevel | LogType.LOG) == this._logLevel) {
            this._console.timeEnd(label);
        }
    }
    timeLog(label?: string, ...data: any[]): void {
        if ((this._logLevel | LogType.LOG) == this._logLevel) {
            this._console.timeLog(label, ...data);
        }
    }
    timeStamp(label?: string): void {
        if ((this._logLevel | LogType.LOG) == this._logLevel) {
            this._console.timeStamp(label);
        }
    }
    trace(...data: any[]): void {
        if ((this._logLevel | LogType.LOG) == this._logLevel) {
            this._console.trace(...data);
        }
    }
    warn(...data: any[]): void {
        if ((this._logLevel | LogType.WARN) == this._logLevel) {
            this._console.warn(...data);
        }
    }
}
